package com.example.mercury.login;

import android.app.Activity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.AppCompatButton;
import androidx.fragment.app.Fragment;

import com.alibaba.fastjson.JSON;
import com.example.mercury.R;
import com.example.mercury.entity.User;
import com.example.mercury.utils.HttpUtils;
import com.google.gson.Gson;

import org.jetbrains.annotations.NotNull;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.MediaType;
import okhttp3.RequestBody;
import okhttp3.Response;

/**
 * @author 盛守义
 * @createTime 2021/10/24 16:10
 * @projectName TurningCoffee
 * @className ResetPasswordActivity.java
 * @description TODO
 */
public class ResetPasswordActivity extends AppCompatActivity {
    private View view;
    private Activity activity;

    private EditText et_pwdagain, et_checkpwd;
    private AppCompatButton btn_submitagain;
    private String pwd, pwdagain, email;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_setpwdagain);
        email = getIntent().getStringExtra("mail");
        initViews();
        setListener();
    }

    void initViews(){
        et_pwdagain = findViewById(R.id.et_pwdagain);
        et_checkpwd = findViewById(R.id.lg_truepwdagain);
        btn_submitagain = findViewById(R.id.btn_submitagain);
    }

    class MyOnClickLiastener implements View.OnClickListener{

        @Override
        public void onClick(View view) {
            initValue();
            if(!pwd.equals(pwdagain)){
                Toast.makeText(activity, "两次密码输入不一致", Toast.LENGTH_SHORT).show();
            }else{
                Gson gson = new Gson();
                JSONObject jsonObject = new JSONObject();
                try {
                    jsonObject.put("userTel", email);
                    jsonObject.put("userPassword", pwd);
                } catch (JSONException e) {
                    e.printStackTrace();
                }
//                user.setUserTel(email);
//                user.setUserPassword(pwd);
                String jsonSrting = jsonObject.toString();

                HttpUtils httpUtils = new HttpUtils();
                RequestBody requestBody = RequestBody.create(MediaType.parse("application/json;charset=utf-8"), jsonSrting);
                Call call = httpUtils.getCall(requestBody, "");
                call.enqueue(new Callback() {
                    @Override
                    public void onFailure(@NotNull Call call, @NotNull IOException e) {
                        ResetPasswordActivity.this.runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                Toast.makeText(ResetPasswordActivity.this, "网络连接崩溃了，绝对绝对不是我们的错", Toast.LENGTH_SHORT).show();
                            }
                        });
                    }

                    @Override
                    public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException {

                    }
                });

            }
        }
    }

    void setListener(){
        btn_submitagain.setOnClickListener(new MyOnClickLiastener());
    }

    void initValue(){
        pwd = et_pwdagain.getText().toString().trim();
        pwdagain = et_checkpwd.getText().toString().trim();
    }
}
